#include <iostream>
#include <bits/ostream.tcc>
//
// Created by Asus on 2025/6/1.
//
using namespace std;

class Animal {
public:
    Animal() {
        cout << "Animal::Animal()" << endl;
    };
    virtual void Speak() = 0;
    //析构函数加上virtual关键字，变成虚析构函数
    //virtual ~Animal()
    //{
    //	cout << "Animal虚析构函数调用！" << endl;
    //}
    // virtual ~Animal() = 0;
    virtual ~Animal() {
        cout << "Animal::~Animal()" << endl;
    };
};

// Animal::~Animal()
// {
//     cout << "Animal 纯虚析构函数调用！" << endl;
// }

//和包含普通纯虚函数的类一样，包含了纯虚析构函数的类也是一个抽象类。不能够被实例化。
class Cat : public Animal {
public:
    string *m_Name;
    Cat(string name) {
        cout << "Cat::Cat()" << endl;
        m_Name = new string(name);
    }
    virtual void Speak() {
        cout << "Cat::Speak()" << endl;
    }
    ~Cat() {
        cout << "Cat::~Cat()" << endl;
        if (this->m_Name != NULL) {
            delete this->m_Name;
            m_Name = NULL;
        }
    }
};

void demo01() {
    Animal *animal = new Cat("Tom");
    animal->Speak();
    //通过父类指针去释放，会导致子类对象可能清理不干净，造成内存泄漏
    //怎么解决？给基类增加一个虚析构函数
    //虚析构函数就是用来解决通过父类指针释放子类对象
    delete animal;
}

int main(int argc, char *argv[]) {
    demo01();
    return 0;
}
